//
//  MDHCommonViewModel.swift
//  JSTech
//
//  Created by 高得华 on 2019/5/8.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit


///
class MDHCommonViewModel: MDHTableViewModel {
    override init(services: MDHViewModelServices, params: [AnyHashable : Any]) {
        super.init(services: services, params: params)
    }
    override init() {
        super.init()
    }
    
    override func initialize() {
        super.initialize()
        /// 选中cell的命令
        /// UI Test
        self.didSelectCommand = RACCommand.init(signal: { [weak self] (x) -> RACSignal<AnyObject> in
            if let weakSelf = self {
                let indexPath = x as? IndexPath ?? IndexPath()
                let groupViewModel = weakSelf.dataSource[indexPath.section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
                let itemViewModel = groupViewModel.itemViewModels[indexPath.row]
                if itemViewModel.operation != nil {/// 有操作执行操作
                    itemViewModel.operation!()
                }else if itemViewModel.destViewModelClass.count > 0 {
                    /// 没有操作就跳转VC
                    let ns = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""
                    let clsName = ns+"."+itemViewModel.destViewModelClass
                    let vm = (NSClassFromString(clsName) as? MDHViewModel.Type)?.init(services: weakSelf.services, params: [MHViewModelTitleKey:itemViewModel.title]) ?? MDHViewModel()
                    weakSelf.services.push(vm, animated: true)
                }
            }
            return RACSignal.empty()
        })
    }
}
